<?php
class period extends head {
 function period() {
  $this->table = "period";
  $this->getmetadata();
  $this->maintainhist = false;
  $this->header[2] = "";
  $_SESSION[$this->table] = $this;
 }

 function applyrefs() {
  $this->valids = array_fill(0,$this->atts,"");
  $this->lists = array_fill(0,$this->atts,"");
  $this->refs = array_fill(0,$this->atts,"");
 }

 function selectlist($condition) {
  // Last search cached for speed.
  if ($condition == $this->lastsearch) {
    return $this->lastsearchresults;
  } else {
	  $stmt = "select itemid,name, firstday ,lastday from ".$this->table;
	  $words = explode(" ",$condition);
	  if ($condition !=="" and $condition !=="ALL") {
	   if (sizeof($words > 0)) {
	    $stmt .= " where name like '%$words[0]%'";
	    for ($i=1;$i<sizeof($words);$i++) {
	     $stmt .= " and name like '%$words[$i]%'";
	    }
	   } else {
	    $stmt .= " where name like '%$condition%'";
	   }
	  }
	  $stmt .= " order by editdate desc limit 1000";
	  $ans = sqlreadarray($stmt);
	  // Convert dates to local
	  for ($i=0;$i<sizeof($ans);$i++) {
	   $ans[$i][2] = isotoout($ans[$i][2]);
	   $ans[$i][3] = isotoout($ans[$i][3]);
	  }
	  //
	  $this->lastsearchresults = $ans;
	  $this->lastsearch = $condition;
	  $this->lastsearchheader = "<tr><td>Id</td><td>Name</td><td>First Day</td><td>Last Day</td></tr>";
    return $ans;
  }
 }
 
 
 function createblank() {
  $this->header = array_fill(0,$this->atts,"");
  $this->valids = array_fill(0,$this->atts,"");
  $this->lists = array_fill(0,$this->atts,"");
  $this->refs = array_fill(0,$this->atts,"");
  $this->header[2] = "0";
 }

 function applyfromrequest() {
  // Mapped requests f1-> field 1 etc
  
  $this->valid = true;
  $this->valids = array_fill(0,$this->atts,"");

  if (isset($_REQUEST["f0"]) && $this->header[2] == "0") {
   $size = $this->metadata[0]->length ;  
   $f = $_REQUEST["f0"];
   if(preg_match("/^[0-9A-Z\.-]{0,$size}$/",$f)) {
    $exists = freaditem($this->table,$f) ;
    if ($exists == "") {
     $this->header[0] = $f;
    } else {
     $this->valids[0] = "That ID is already in use for ".$exists[1];
    }
   } else {
    $this->valids[0] = "period code should be made up of numbers and uppercase letters only";
   }
  }
  
  
  if( $f=@$_REQUEST["f1"]) {
   $size = $this->metadata[1]->length ;
   if(preg_match("/^[0-9 a-z A-Z\'\.-]{0,$size}$/",$f)) {
     $this->header[1] = $f;
   } else {
    $this->valids[1] = "Invalid characters";
   }
  }
  
  $p=5;
	if( $f=@$_REQUEST["f$p"]) {
   // From Date. 
   $a = dtoiso($f);
   if ($a) {
    $diff = isotojd()-isotojd($a) ;
    if ($diff > 3000 || $diff < -3000) {
     $this->valids[$p] = "Date looks wrong";
    } else {
     $this->header[$p] = $a;
    }
   } else {
    $this->valids[$p] = "Invalid date";
   }
  }   
  
  $p=6;
	if( $f=@$_REQUEST["f$p"]) {
   // To Date. 
   $a = $this->header[5];
   if ($a) {
    $f = dtoiso($f);
    $diff = isotojd($f)-isotojd($a) ;
    if ($diff < 0) {
     $this->valids[$p] = "End date must be after start date";
    } else {
     $this->header[$p] = $f;
    }
   } else {
    $this->valids[$p] = "Invalid date";
   }
  }   
  
  if ($this->header[5] == "") {$this->valids[5] = "Please enter period start date";}
  if ($this->header[6] == "") {$this->valids[6] = "Please enter period end date";}
    
  $this->valid = every($this->valids,""); 

  // Periods must not overlap. i.e. existing period start date must not be between
  // and existing period end date must not be between
  if ($this->valid) {
    $sql = "select itemid,name,firstday,lastday from period where ";
		$sql .= "(    (firstday >= '{$this->header[5]}' and firstday <= '{$this->header[6]}' )";
    $sql .= " or (lastday >= '{$this->header[5]}' and lastday <= '{$this->header[6]}' ))";
    if ($this->header[2] != "0") {
     $sql .= " and itemid <> '{$this->header[0]}'";
		}
		
    $res = sqlread($sql);
    if ($res) { 
     $this->valid = false;
     $this->valids[5] = "Date range overlaps with ".$res[1]. "(".isotoout($res[2])." to ".isotoout($res[3]).")" ;
    }
	}

 }
 
 function showform() {
   $readonly = "readonly"; $focalpoint = "f1";

  if ($this->header[2] == "0") { $focalpoint = "f0"; $readonly = "";} 
 
  echo "<head>";
  echo "<script src='datechooser.js' type='text/javascript'></script>"; 
  echo SCRIPTS;
  echo "</head>";
	echo "<body onload='document.form.".$focalpoint.".focus()'>";
  echo "<h1>Period Setup</h1>"; 
  echo "<form name='form' method=POST>";
  echo "<input type=hidden name=res value=''>";
  echo "<table>";
  
  if ($this->header[2] == "0") {
   echo $this->htmlrow("0","Period identifier");
  } else {
   echo "<tr><td>Period Identifier</td><td>".$this->header[0]."</td></tr>";
  }
  
  echo $this->htmlrow("1","Description");

  // Includes callout date selector
  echo "<tr><td>Start Date</td><td>";
  $dt = isotoout($this->header[5]);
  echo "<input $this->disabled id=dot name=f5 size=10 maxlength=10 type=text value='$dt'>\n"; 
  $dc = "showChooser(this, 'dot', 'chooserSpan', 1950, 2010, 'd-m-Y', false);";
  echo "<img src='calendar.gif' onclick=".'"'.$dc.'"'.">\n";
  echo "<div id='chooserSpan' class='dateChooser' style='display: none; visibility: hidden; width: 160px;'>\n";
  echo "</div>\n";
  echo "</td>";
  echo "<td>".$this->valids[5]."</td>";  
  echo "</tr>";

  echo "<tr><td>End Date</td><td>";
  $dt = isotoout($this->header[6]);
  echo "<input $this->disabled id=dot2 name=f6 size=10 maxlength=10 type=text value='$dt'>\n"; 
  $dc = "showChooser(this, 'dot2', 'chooserSpan', 1950, 2010, 'd-m-Y', false);";
  echo "<img src='calendar.gif' onclick=".'"'.$dc.'"'.">\n";
  echo "<div id='chooserSpan' class='dateChooser' style='display: none; visibility: hidden; width: 160px;'>\n";
  echo "</div>\n";
  echo "</td>";
  echo "<td>".$this->valids[6]."</td>";  
  echo "</tr>";
  
  echo "</table>";
  echo "<input class=bt accesskey=S type=submit name=sub value=Save>";
  echo "<input class=bt accesskey=A type=submit name=sub value=Apply>";
  echo "<input class=bt accesskey=D type=submit name=sub value=Delete>";
  echo "<button class=bt onclick=rest()><span>Restart</span></button>";
  echo "</form>"; 
  // set up help
  echo $this->registerhelp(); 
 }
 
}
?>